package com.autoplaza;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;

public class Grafica extends Activity {
	int width,height;
	private ProgressDialog progressDialog;
	WebView web;
	String idanuncio;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.grafica);
		
		if(isOnline()){
			new CargarInfo().execute("");
		}else{
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.necesita_conexi_n_a_internet)
			.setCancelable(false)
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					finish();
					}
			});
			AlertDialog alertDialog = builder.create();
			alertDialog.show();
		}
		
		
		
	}

	
	
	public class CargarInfo extends AsyncTask<String, String, String>{

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog = ProgressDialog.show(Grafica.this, "",
					"Cargando...", true, true, new OnCancelListener() {
						
						@Override
						public void onCancel(DialogInterface arg0) {
							finish();
						}
			});
		}
		
		@Override
		protected String doInBackground(String... params) {
			DisplayMetrics metrics = new DisplayMetrics();
			getWindowManager().getDefaultDisplay().getMetrics(metrics);

			height = metrics.heightPixels;
			width = metrics.widthPixels;
			
//			 Display display = getWindowManager().getDefaultDisplay(); 
//		        width = display.getWidth();  // deprecated
//		        height = display.getHeight();  // deprecated
		        
		        Log.i("WIDTH", ""+width);
		        Log.i("HEIGHT", ""+height);
			
			Bundle extras=getIntent().getExtras();
			if(extras != null){
				idanuncio=extras.getString("numanuncio");
				
//				webSettings.setLoadWithOverviewMode(true);
//				webSettings.setUseWideViewPort(true);
				//http://www.autos-usados.autoplaza.com.mx/Autos/GeneraGraficosPage.aspx?Id=2035561&w=960&h=720
				
				
//				web.loadUrl("http://beta.autoplaza.com.mx/Autos/WebChartPage.aspx?Id="+idanuncio);
//				Log.i("URL", "http://beta.autoplaza.com.mx/Autos/WebChartPage.aspx?Id="+idanuncio);
			}
			
			return null;
		}
		
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			web=(WebView) findViewById(R.id.webView1);
			WebSettings webSettings = web.getSettings();
			webSettings.setJavaScriptEnabled(true);
			webSettings.setSupportZoom(true);
			webSettings.setBuiltInZoomControls(true);
			webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
			web.loadUrl("http://www.autos-usados.autoplaza.com.mx/Autos/GeneraGraficosPage.aspx?Id="+idanuncio+"&w="+width+"&h="+height);
			Log.i("URL","http://www.autos-usados.autoplaza.com.mx/Autos/GeneraGraficosPage.aspx?Id="+idanuncio+"&w="+width+"&h="+height);
			progressDialog.dismiss();

		}
		
		
	}
	
	//CHECA LA CONEXION A INTERNET
	public boolean isOnline() {
		NetworkInfo networkInfo = ((ConnectivityManager) getApplicationContext()
	              .getSystemService(Context.CONNECTIVITY_SERVICE))
	              .getActiveNetworkInfo();

	            if(networkInfo == null || !networkInfo.isConnected()) {
	             return false;
	            }
	            return true;
	}

	
	
	
	
}
















